<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class Core_EventController extends Zend_Controller_Action
{
    const SHOW_FUTURE = 'future';
    const SHOW_MINE = 'mine';

    private $_eventService;
    private $_shiftService;

    public function init()
    {
        $this->_eventService = $this->_helper->getService('Core_Service_Event');
        $this->_shiftService = $this->_helper->getService('Core_Service_Shift');
        $this->_interestService = $this->_helper->getService('Core_Service_Interest');
    }

    public function indexAction()
    {
        $filterContent = array(
            self::SHOW_FUTURE => 'Alle fremtidige arrangementer',
            self::SHOW_MINE   => 'Alle mine arrangementer',
        );

        $filterForm = $this->view->filterForm = $this->view->filterForm($filterContent);
        $filterForm->setAction($this->view->url(array('module' => 'core', 'controller' => 'event'), null, true));

        $request = $this->getRequest();
        if ($request->isGet() && $this->_getParam('filter', false) && $filterForm->isValid($request->getQuery())) {
            $value = $filterForm->getValue('filter');

            $memberId = $this->view->authInfo()->getMemberId();
            switch ($value) {
                case self::SHOW_FUTURE:
                    $this->view->events = $this->_eventService->fetchAllFutureForUser(false);
                    break;
                case self::SHOW_MINE:
                    $this->view->events = $this->_eventService->fetchAllByUser($memberId, false);
                    break;
            }
        }

        if (!isset($this->view->events)) {
            $this->view->events = $this->_eventService->fetchAllFutureForUser(false);
        }
    }

  public function viewAction()
  {
    $this->_helper->acl()->isAllowed('action:core.event', 'view');

    $this->view->isAdmin = $this->_helper->acl()->isAllowed('service:event', 'edit', false);
        $this->view->canCreateShift = $this->_helper->acl()->isAllowed('service:shift', 'create', false);
    $eventId = $this->_getParam('id');

    $validator = new Zend_Validate_Int();
    if (!$validator->isValid($eventId)) {
      throw new RedCross_Exception_ValidationFailed();
    }

    $eventId = (int)$eventId;
    $this->view->eventId = $eventId;

    $event = $this->_eventService->fetch($eventId);
    if (!$event) {
      throw new RedCross_Exception_NotFound();
        }
    $this->view->event = $event;

    $authService = $this->_helper->authenticator();
    $memberId = $authService->getIdentity()->member_id;
    $this->view->hasInterest = $this->_interestService->checkIfMemberHasInterestForEvent($memberId, $eventId);

    //$shifts = $this->_shiftService->fetchByEventId($eventId);
    $shifts = array();
    foreach ($event->Shifts as $shift)
      $shifts[] = $shift;
    usort($shifts, array('RedCross_Global', 'shift_sort_cmp'));

    $shiftEvent = "Vaktarrangement";
    if ($this->view->isAdmin && ($event->type == $shiftEvent)) {
      Core_Service_Event::check_shift_coverage($this->view, $event, $shifts);
    }

    $this->view->shifts = $shifts;
  }

    public function toggleinterestAction()
    {
        $authService = $this->_helper->authenticator();
        $memberId = $authService->getIdentity()->member_id;
        $request = $this->getRequest();
        $params = $request->getParams();
        $eventId = $params['id'];
        $response = $this->_interestService->toggleInterest($memberId,$eventId);
        $this->view->hasInterest = $hasInterest;
        return $this->_helper->redirector('view', 'event', 'core', array('id' => $eventId));

    }
}
